package com.guojiaming.shop.controller;

import com.guojiaming.shop.bean.*;
import com.guojiaming.shop.service.MemberService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @author vivo
 */
@RestController
@RequestMapping("member")
public class MemberController {
    @Resource
    private MemberService memberService;

    @GetMapping("all")
    public ResponseBody<ListBody<Member>> getAll() {
        return ResponseFactory.list(memberService.findAll());
    }

    @GetMapping("save")
    public ResponseBody<Member> saveMember(@Valid Member member) {
        return ResponseFactory.success(memberService.addMember(member), "保存成功");
    }

    @GetMapping("delete")
    public ResponseBody<Integer> deleteMember(@Valid @RequestParam(value = "memberId") Integer memberId) {
        return ResponseFactory.success(memberService.deleteMember(memberId), "删除成功");
    }

    @GetMapping("update")
    public ResponseBody<Member> updateMember(@Valid MemberModify memberModify) {
        return ResponseFactory.success(memberService.updateMember(memberModify), "修改成功");
    }

    @GetMapping("find")
    public ResponseBody<Member> selectMember(@Valid @RequestParam(value = "memberId") Integer memberId) {
        return ResponseFactory.success(memberService.selectMember(memberId));
    }
}
